//
//  SwiftUIView.swift
//  TomatoClockWidgetExtension
//
//  Created by DaleGuo on 2023/11/9.
//

import ActivityKit
import SwiftUI
import WidgetKit

struct ActiveCountDownView: View {
    let context: ActivityViewContext<CountDonwAttributes>

    @State var currentTime: Date = .init()

    var body: some View {
        VStack(alignment: .leading) {
            HStack {
                Text(context.attributes.timerName)
                    .font(.system(size: 13))
                
                Spacer()
                
                Text("Never Settel ")
                    .italic()
                    .bold()
                    .lineLimit(1)
            }
            ProgressView(timerInterval: Date() ... context.state.endTime)
        }

        .padding()
    }

    func getMinutesDifferenceFromTwoDates(start: Date, end: Date) -> Double {
        let diff = Int(end.timeIntervalSince1970 - start.timeIntervalSince1970)

        let hours = diff / 3600
        let minutes = (diff - hours * 3600) / 60

        return Double(minutes) + 0.001
    }
}

struct ActiveCountDownWidget: Widget {
    let kind: String = "countDownWidget"

    var body: some WidgetConfiguration {
        ActivityConfiguration(for: CountDonwAttributes.self) { context in
            // 不支持灵动岛则
            ActiveCountDownView(context: context)
        } dynamicIsland: { context in
            // 灵动岛
            DynamicIsland {
                DynamicIslandExpandedRegion(.leading) {
                    Text("🍅")
                        .font(.largeTitle)
                }

                DynamicIslandExpandedRegion(.trailing) {
                    Text(context.state.endTime, style: .timer)
                        .italic()
                        .font(.largeTitle)
                }

                DynamicIslandExpandedRegion(.bottom) {
                    ProgressView(timerInterval: Date() ... context.state.endTime)
                }

            } compactLeading: {
                Text("🍅")
            } compactTrailing: {
                HStack {
                    Spacer()
                    Text(context.state.endTime, style: .timer)
                        .italic()
                }
            } minimal: {}
        }
    }
}
